package com.tj.admin.auth.service;


import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.qianli.cashmama.trade.client.bill.BillApplicationService;
import com.qianli.cashmama.trade.client.trade.TradeQueryService;
import com.qianli.cashmama.trade.client.trade.request.UserTradeListQueryRO;
import com.qianli.cashmama.trade.client.trade.response.TradeInfoRO;
import com.qianli.cashmama.trade.common.enums.TradeStatusEnum;
import com.tj.admin.auth.response.TopUpDTO;
import com.tj.admin.auth.vo.*;
import com.tj.admin.db.dao.IncomeDetailDao;
import com.tj.admin.db.dao.SpendingDetailDao;
import com.tj.admin.db.entity.IncomeDetailEntity;
import com.tj.admin.db.entity.SpendingDetailEntity;
import com.tj.admin.auth.response.StatementDTO;
import com.tj.bo.common.conf.api.ApiResponse;
import com.tj.bo.common.conf.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FinancialApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(FinancialApplication.class);

    @Autowired
    private SpendingDetailDao spendingDetailDao;

    @Autowired
    private IncomeDetailDao incomeDetailDao;

    @Autowired
    private BillApplicationService billApplicationService;

    @Autowired
    private TradeQueryService tradeQueryService;

    public List<ReimbursementVo> queryReimbursementList(int currentPage, String productName,String startTime,String endTime){

        ArrayList<ReimbursementVo> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new ReimbursementVo("2021-10-22","https://t7.baidu.com/it/u=848096684,3883475370&fm=193&f=GIF",
                    "CashLoan","500","3000","20%",
                    "5000","6000","88%","4444","55555","66%"));
        }
        return list;
    }

    public List<LendingVo> queryLendingList(int currentPage, String productName, String startTime, String endTime){
        UserTradeListQueryRO userTradeListQueryRO = new UserTradeListQueryRO();
        userTradeListQueryRO.setNeedCardInfo(false);
        userTradeListQueryRO.setUserCode(null);
        userTradeListQueryRO.setOrderStatus(null);
//        userTradeListQueryRO.setMerchantCode(merchantCode);
        Response<List<TradeInfoRO>> listResponse = tradeQueryService.queryTradeListByUserCodeAndOrderStatus(userTradeListQueryRO);

        ArrayList<LendingVo> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new LendingVo("2021-10-22","https://t7.baidu.com/it/u=848096684,3883475370&fm=193&f=GIF",
                    "CashBean","10000","8000","80%",
                    "5000","4500","90%","15000","12500","75%"));
        }
        return list;
    }

    public ApiResponse<StatementDTO> queryStatementList(Page page, String startTime, String endTime){
        StatementDTO statementDTO = new StatementDTO();
        long sTime;
        Date eTime;
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
           sTime = DateUtil.getDate(startTime, DateUtil.getDatePattern()).getTime();
           eTime = DateUtil.getDate(endTime, DateUtil.getDatePattern());
        }else if (!StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
            sTime = DateUtil.getDate(startTime, DateUtil.getDatePattern()).getTime();
            eTime = DateUtils.getNextWeekend(DateUtil.getDate(startTime, DateUtil.getDatePattern()));
        } else if (StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            sTime = DateUtils.getLastWeekend(DateUtil.getDate(endTime, DateUtil.getDatePattern())).getTime();
            eTime = DateUtil.getDate(endTime, DateUtil.getDatePattern());
        }else {
            sTime = DateUtils.getLastWeekend(new Date()).getTime();
            eTime = DateUtil.getTodayOverTime();
        }
        long eDate = DateUtil.addDate(eTime, 1).getTime();
        List<SpendingDetailEntity> listByPage = spendingDetailDao.selectListByPage(page,sTime/1000,eDate/1000);
        ArrayList<StatementVo> list = Lists.newArrayList();
        listByPage.forEach(item ->{
            String date = DateUtil.getDate(new Date(Long.parseLong(item.getSpendingDate()+"")*1000), DateUtil.getDatePattern());
            list.add(new StatementVo(date,item.getProductName(),item.getContent(),item.getAmount()));
        });
        statementDTO.setList(CollectionUtils.isEmpty(list) ? null: list);
        page.setTotalCount(spendingDetailDao.selectSpendingCount());
        return new ApiResponse<>(statementDTO).setPage(page);
    }

    public ApiResponse<TopUpDTO> queryTopUpList(Page page,  String startTime, String endTime){
        TopUpDTO topUpDTO = new TopUpDTO();
        long sTime;
        Date eTime;
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            sTime = DateUtil.getDate(startTime, DateUtil.getDatePattern()).getTime();
            eTime = DateUtil.getDate(endTime, DateUtil.getDatePattern());
        }else if (!StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
            sTime = DateUtil.getDate(startTime, DateUtil.getDatePattern()).getTime();
            eTime = DateUtils.getNextWeekend(DateUtil.getDate(startTime, DateUtil.getDatePattern()));
        } else if (StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            sTime = DateUtils.getLastWeekend(DateUtil.getDate(endTime, DateUtil.getDatePattern())).getTime();
            eTime = DateUtil.getDate(endTime, DateUtil.getDatePattern());
        }else {
            sTime = DateUtils.getLastWeekend(new Date()).getTime();
            eTime = DateUtil.getTodayOverTime();
        }
        long eDate = DateUtil.addDate(eTime, 1).getTime();
        List<IncomeDetailEntity> listByPage = incomeDetailDao.selectListByPage(page,sTime/1000,eDate/1000);
        ArrayList<TopUpVo> list = Lists.newArrayList();
        listByPage.forEach(item ->{
            String date = DateUtil.getDate(new Date(Long.parseLong(item.getIncomeDate()+"")*1000), DateUtil.getDatePattern());
            list.add(new TopUpVo(date,item.getIncomeWay(),item.getIncomeCredentials(),item.getAmount()));
        });
        topUpDTO.setList(CollectionUtils.isEmpty(list) ? null: list);
        page.setTotalCount(incomeDetailDao.selectIncomeCount());
        return new ApiResponse<>(topUpDTO).setPage(page);
    }

    public IndexInfoVo queryIndex(){
       List<String> lList = new ArrayList<>();
        lList.add("100");
        lList.add("200");
        lList.add("300");
        lList.add("400");
        lList.add("500");
        lList.add("600");
        lList.add("700");
        ArrayList<String> rList = new ArrayList<>();
        rList.add("500");
        rList.add("700");
        rList.add("800");
        rList.add("900");
        rList.add("1000");
        rList.add("1200");
        rList.add("1500");
        IndexInfoVo indexInfoVo = new IndexInfoVo("1000","10","300","1232",
                "54%","1111","11111","22222","46%","10%",
                rList,lList
                );
        return indexInfoVo;
    }
}
